import scrapy


class LoginSpider(scrapy.Spider):
    name = "login"


    # def start_requests(self):
    #     home_page = "https://quotes.toscrape.com/"
    #     yield scrapy.Request(url=home_page, cookies={
    #         "session": "eyJjc3JmX3Rva2VuIjoiU2RGb013TGJCcWNleE9BbFJOa2hhcHJWdUNUbkl5RFhFWWdQUW1zVVpLdHZ6ZkhXaWpHSiIsInVzZXJuYW1lIjoiMiJ9.ZzGnmw.c9ypZC2BludEiDocohne9f1hy3A"
    #     })
    #
    # def parse(self, response):
    #     print(response.text)


    def start_requests(self):
        login = "https://quotes.toscrape.com/login"
        yield scrapy.Request(url=login, )

    def parse(self, response):
        # print(response.text)

        token = response.css("[type='hidden']::attr('value')").get()

        yield scrapy.FormRequest(url="https://quotes.toscrape.com/login", formdata={
            "csrf_token": token,
            "username": "admin",
            "password": "123456"
        }, callback=self.login_parse)

    def login_parse(self, response):

        yield scrapy.Request(url="https://quotes.toscrape.com/page/2/", callback=self.parse3)

    def parse3(self, response):
        print(response.text)

